﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Orbitone.MeetingRooms.RoomFinder.UserControls.GridView;
using Orbitone.MeetingRooms.Services.Model;

namespace Orbitone.MeetingRooms.RoomFinder.UserControls
{
    public partial class Calendar : UserControl
    {
        public EventHandler CalendarItemClick;
        public string UserId { get; set; }

        public List<AvailabilityModel> Availability { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var wrapper = new GridViewWrapper(dgDataGrid);
            wrapper.gvt_CalendarItemClick += CalendarItemClick;
            FillWrapper(wrapper, Availability, StartDate, EndDate);
            wrapper.DataBind();

            dgDataGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
            dgDataGrid.CssClass = "calendartable";
        }

        private void FillWrapper(GridViewWrapper wrapper, IEnumerable<AvailabilityModel> availability,
                                 DateTime startDate, DateTime endDate)
        {
            wrapper.AddColumn("Time");
            foreach (AvailabilityModel model in availability)
            {
                wrapper.AddColumn(model.Mailbox.DisplayName);
            }

            int row = 0;
            for (DateTime htStartTime = startDate; htStartTime < endDate; htStartTime = htStartTime.AddMinutes(30))
            {
                DateTime startInterval = htStartTime;
                DateTime endInterval = htStartTime.AddMinutes(30);

                wrapper.AddRow();
                string time = htStartTime.ToString("HH:mm");
                wrapper.SetLabel(row, 0, time);

                int mailBoxNr = 0;
                foreach (AvailabilityModel model in availability)
                {
                    List<CalendarEventModel> events = model.CalendarEvents;

                    foreach (CalendarEventModel evt in events)
                    {
                        if (startInterval == evt.StartDate)
                        {
                            int rowspan = (int) (evt.EndDate - evt.StartDate).TotalMinutes/30;

                            if (!string.IsNullOrEmpty(UserId) && UserId.Equals(evt.UserId))
                            {
                                wrapper.SetLinkButton(row,
                                                      mailBoxNr + 1,
                                                      string.IsNullOrEmpty(evt.UserName)
                                                          ? evt.Subject
                                                          : evt.UserName + ": " + evt.Subject,
                                                      evt.Subject + " - " + evt.StartDate.ToString("HH:mm") + " to " +
                                                      evt.EndDate.ToString("HH:mm"),
                                                      evt.Id,
                                                      evt.Mailbox,
                                                      rowspan);
                            }
                            else
                            {
                                wrapper.SetLabel(row,
                                                 mailBoxNr + 1,
                                                 string.IsNullOrEmpty(evt.UserName)
                                                     ? evt.Subject
                                                     : evt.UserName + ": " + evt.Subject,
                                                 evt.Subject + " - " + evt.StartDate.ToString("HH:mm") + " to " +
                                                 evt.EndDate.ToString("HH:mm"),
                                                 rowspan);
                            }
                        }
                        else if (evt.StartDate <= startInterval && endInterval <= evt.EndDate)
                        {
                            wrapper.SetHiddenItem(row, mailBoxNr + 1);
                        }
                    }
                    mailBoxNr++;
                }
                row++;
            }
            ;
        }
    }
}